<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="Iteration, Iterable, Iterator, Generator, yield in Python迭代(Iteration)迭代，指的是按照某种顺序逐个访问列表中的每一项。
在 Python 中，用for 来实现迭代，它的结构是 for ... in ...，实际是利用可迭代对象Iterable得到迭代器Iterator，再重复调用 __next()__方法实现的。

可迭代对象(">
<meta property="og:type" content="article">
<meta property="og:title" content="Iteration, Iterable, Iterator, Generator, yield in Python">
<meta property="og:url" content="http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/index.html">
<meta property="og:site_name" content="enomine">
<meta property="og:description" content="Iteration, Iterable, Iterator, Generator, yield in Python迭代(Iteration)迭代，指的是按照某种顺序逐个访问列表中的每一项。
在 Python 中，用for 来实现迭代，它的结构是 for ... in ...，实际是利用可迭代对象Iterable得到迭代器Iterator，再重复调用 __next()__方法实现的。

可迭代对象(">
<meta property="og:image" content="http://nvie.com/img/iterable-vs-iterator.png">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/c3c88275gw1f3zbjmfejfj20zc0fytay.jpg">
<meta property="og:updated_time" content="2017-04-25T07:59:17.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Iteration, Iterable, Iterator, Generator, yield in Python">
<meta name="twitter:description" content="Iteration, Iterable, Iterator, Generator, yield in Python迭代(Iteration)迭代，指的是按照某种顺序逐个访问列表中的每一项。
在 Python 中，用for 来实现迭代，它的结构是 for ... in ...，实际是利用可迭代对象Iterable得到迭代器Iterator，再重复调用 __next()__方法实现的。

可迭代对象(">
<meta name="twitter:image" content="http://nvie.com/img/iterable-vs-iterator.png">
    
    
        
          <link rel="shortcut icon" href="/images/favicon.ico">
        
        
          <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
        
        
          <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
        
    
    <!-- title -->
    <title>Iteration, Iterable, Iterator, Generator, yield in Python</title>
    <!-- styles -->
    <link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/lib/meslo-LG/styles.css">
    <link rel="stylesheet" href="/lib/justified-gallery/justifiedGallery.min.css">
    <link rel="stylesheet" href="/css/style.css">
    <!-- rss -->
    
    
    <!-- jquery -->
    <script src="/lib/jquery/jquery.min.js"></script>
</head>

<body>
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fa fa-bars fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="http://github.com/enominecn">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
      <ul>
        
        <li><a class="icon" href="/2017/04/24/Iteration,Iterable,Iterator,Generator,yield-in-Python/"><i class="fa fa-chevron-left" aria-hidden="true" onmouseover='$("#i-prev").toggle();' onmouseout='$("#i-prev").toggle();'></i></a></li>
        
        
        <li><a class="icon" href="/2017/04/17/Decorators-for-Class/"><i class="fa fa-chevron-right" aria-hidden="true" onmouseover='$("#i-next").toggle();' onmouseout='$("#i-next").toggle();'></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fa fa-chevron-up" aria-hidden="true" onmouseover='$("#i-top").toggle();' onmouseout='$("#i-top").toggle();'></i></a></li>
        <li><a class="icon" href="#"><i class="fa fa-share-alt" aria-hidden="true" onmouseover='$("#i-share").toggle();' onmouseout='$("#i-share").toggle();' onclick='$("#share").toggle();return false;'></i></a></li>
      </ul>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
        <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/"><i class="fa fa-facebook" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="https://twitter.com/share?url=http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/&text=Iteration, Iterable, Iterator, Generator, yield in Python"><i class="fa fa-twitter" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/&title=Iteration, Iterable, Iterator, Generator, yield in Python"><i class="fa fa-linkedin" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/&is_video=false&description=Iteration, Iterable, Iterator, Generator, yield in Python"><i class="fa fa-pinterest" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="mailto:?subject=Iteration, Iterable, Iterator, Generator, yield in Python&body=Check out this article: http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/"><i class="fa fa-envelope" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="https://getpocket.com/save?url=http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/&title=Iteration, Iterable, Iterator, Generator, yield in Python"><i class="fa fa-get-pocket" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="http://reddit.com/submit?url=http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/&title=Iteration, Iterable, Iterator, Generator, yield in Python"><i class="fa fa-reddit" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/&title=Iteration, Iterable, Iterator, Generator, yield in Python"><i class="fa fa-stumbleupon" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="http://digg.com/submit?url=http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/&title=Iteration, Iterable, Iterator, Generator, yield in Python"><i class="fa fa-digg" aria-hidden="true"></i></a></li>
        <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://enomineCN.github.io/2017/04/24/Iteration, Iterable, Iterator, Generator, yield in Python/&name=Iteration, Iterable, Iterator, Generator, yield in Python&description="><i class="fa fa-tumblr" aria-hidden="true"></i></a></li>
      </ul>
    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Iteration-Iterable-Iterator-Generator-yield-in-Python"><span class="toc-number">1.</span> <span class="toc-text">Iteration, Iterable, Iterator, Generator, yield in Python</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#迭代-Iteration"><span class="toc-number">1.1.</span> <span class="toc-text">迭代(Iteration)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#可迭代对象-Iterable"><span class="toc-number">1.2.</span> <span class="toc-text">可迭代对象(Iterable)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#迭代器-Iterator"><span class="toc-number">1.3.</span> <span class="toc-text">迭代器(Iterator)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#生成器-Generator"><span class="toc-number">1.4.</span> <span class="toc-text">生成器(Generator)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#两种创建方式"><span class="toc-number">1.4.1.</span> <span class="toc-text">两种创建方式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#包含yield的函数-generator-function"><span class="toc-number">1.4.1.1.</span> <span class="toc-text">包含yield的函数(generator function)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#生成器表达式-generator-expression"><span class="toc-number">1.4.1.2.</span> <span class="toc-text">生成器表达式(generator expression)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#三个内置方法"><span class="toc-number">1.4.2.</span> <span class="toc-text">三个内置方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#close"><span class="toc-number">1.4.2.1.</span> <span class="toc-text">close()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#send"><span class="toc-number">1.4.2.2.</span> <span class="toc-text">send()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#throw"><span class="toc-number">1.4.2.3.</span> <span class="toc-text">throw()</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#todo"><span class="toc-number">2.</span> <span class="toc-text">todo</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#参考资料"><span class="toc-number">2.1.</span> <span class="toc-text">参考资料</span></a></li></ol></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index width mx-auto px2 my4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        Iteration, Iterable, Iterator, Generator, yield in Python
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">enomine</span>
      </span>
      
    <div class="postdate">
        <time datetime="2017-04-24T11:11:25.000Z" itemprop="datePublished">2017-04-24</time>
    </div>


      
    <div class="article-tag">
        <i class="fa fa-tag"></i>
        <a class="tag-link" href="/tags/generator/">generator</a>, <a class="tag-link" href="/tags/iterable/">iterable</a>, <a class="tag-link" href="/tags/iteration/">iteration</a>, <a class="tag-link" href="/tags/iterator/">iterator</a>, <a class="tag-link" href="/tags/yield/">yield</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h1 id="Iteration-Iterable-Iterator-Generator-yield-in-Python"><a href="#Iteration-Iterable-Iterator-Generator-yield-in-Python" class="headerlink" title="Iteration, Iterable, Iterator, Generator, yield in Python"></a>Iteration, Iterable, Iterator, Generator, yield in Python</h1><h2 id="迭代-Iteration"><a href="#迭代-Iteration" class="headerlink" title="迭代(Iteration)"></a>迭代(Iteration)</h2><p><strong>迭代</strong>，指的是按照某种顺序逐个访问列表中的每一项。</p>
<p>在 Python 中，用<code>for</code> 来实现迭代，它的结构是 <code>for ... in ...</code>，实际是利用可迭代对象<code>Iterable</code>得到迭代器<code>Iterator</code>，再重复调用 <code>__next()__</code>方法实现的。</p>
<p><img src="http://nvie.com/img/iterable-vs-iterator.png" alt=""></p>
<h2 id="可迭代对象-Iterable"><a href="#可迭代对象-Iterable" class="headerlink" title="可迭代对象(Iterable)"></a>可迭代对象(Iterable)</h2><p>可迭代对象<code>Iterable</code>就是实现了<code>__iter__</code>方法，可以返回一个迭代器<code>Iterator</code>，或者定义了<code>__getitem__</code>方法，可以按<code>index</code> 索引（且能够在没有值时抛出一个 <code>IndexError</code> 异常）的任意对象。</p>
<blockquote>
<p><a href="http://stackoverflow.com/questions/9884132/what-exactly-are-pythons-iterator-iterable-and-iteration-protocols" target="_blank" rel="external"><strong>Stackoverflow:</strong></a></p>
<p>An <strong>iterable</strong> is an object that has an <code>__iter__</code> method which returns an <strong>iterator</strong>, or which defines a <code>__getitem__</code> method that can take sequential indexes starting from zero (and raises an <code>IndexError</code> when the indexes are no longer valid). So an <strong>iterable</strong> is an object that you can get an <strong>iterator</strong> from.</p>
</blockquote>
<p>可迭代对象具有如下的特性：</p>
<ul>
<li>可以<code>for</code>循环： <code>for i in iterable</code></li>
<li>可以按<code>index</code>索引：定义了<code>__getitem__</code>方法，比如<code>list,str</code></li>
<li>可以通过调用<code>__iter__()</code>方法或者被<code>iter(obj)</code>调用，返回一个迭代器<code>Iterator</code></li>
</ul>
<p>可以通过<code>isinstance(obj, collections.Iterable)</code> 来判断对象是否为可迭代对象。</p>
<p>&gt;</p>
<blockquote>
<p><strong>NOTE:</strong><br>Often, for pragmatic reasons, iterable classes will implement both <code>__iter__()</code> and <code>__next__()</code> in the same class, and have <code>__iter__()</code> return <code>self</code>, which makes the class both an iterable and its own iterator. It is perfectly fine to return a different object as the iterator, though.</p>
</blockquote>
<h2 id="迭代器-Iterator"><a href="#迭代器-Iterator" class="headerlink" title="迭代器(Iterator)"></a>迭代器(Iterator)</h2><p>迭代器<code>Iterator</code>就是实现了<code>next (Python 2)</code> 或者 <code>__next__ (Python 3)</code> 方法（并且能够在没有值时抛出一个 <code>StopIteration</code>异常）的任意对象。</p>
<p>如果需要自定义迭代器，则需要满足如下迭代器协议：</p>
<ul>
<li>定义了<code>__iter__</code>方法，但是必须返回自身</li>
<li>定义了<code>__next__</code>方法。用来返回下一个值，并且当没有数据了，抛出 <code>StopIteration</code></li>
<li>可以保持当前的状态</li>
</ul>
<p>可以通过 <code>isinstance(obj, collections.Iterator)</code> 来判断对象是否为迭代器。</p>
<h2 id="生成器-Generator"><a href="#生成器-Generator" class="headerlink" title="生成器(Generator)"></a>生成器(Generator)</h2><p>生成器<code>Generator</code>是创建一个迭代器最简单的方式。在普通的函数中使用<code>yield</code>代替<code>return</code>，这个函数将会返回一个生成器。</p>
<p>生成器是可以迭代的，但是你 <strong>只可以读取它一次</strong> ，因为它并不把所有的值放在内存中，而是实时地生成数据。</p>
<p><img src="http://ww4.sinaimg.cn/mw690/c3c88275gw1f3zbjmfejfj20zc0fytay.jpg" alt=""></p>
<h3 id="两种创建方式"><a href="#两种创建方式" class="headerlink" title="两种创建方式"></a>两种创建方式</h3><h4 id="包含yield的函数-generator-function"><a href="#包含yield的函数-generator-function" class="headerlink" title="包含yield的函数(generator function)"></a>包含<code>yield</code>的函数(generator function)</h4><p>yield 是一个类似 return 的关键字，只是这个函数返回的是个生成器。当你调用这个函数的时候，函数内部的代码并不立马执行 ，这个函数只是返回一个生成器对象。</p>
<p>生成器函数跟普通函数只有一点不一样，就是把 <code>return</code> 换成<code>yield</code>,其中<code>yield</code>是一个语法糖，内部实现了迭代器协议，同时保持状态可以挂起。<code>yield</code>是数据的生产者，而诸如<code>for</code>等是数据的消费者。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">generator_func</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">yield</span> <span class="string">'step 1'</span></div><div class="line">    <span class="keyword">yield</span> <span class="string">'step 2'</span></div><div class="line">    <span class="keyword">yield</span> <span class="string">'step 3'</span></div><div class="line">    </div><div class="line">gen = generator_func()</div><div class="line">gen.__next__()</div><div class="line"><span class="comment"># step 1</span></div><div class="line">gen.__next__()</div><div class="line"><span class="comment"># step 2</span></div><div class="line">gen.__next__()</div><div class="line"><span class="comment"># step 3</span></div><div class="line">gen.__next__()</div><div class="line"><span class="comment"># StopIteration</span></div></pre></td></tr></table></figure>
<h4 id="生成器表达式-generator-expression"><a href="#生成器表达式-generator-expression" class="headerlink" title="生成器表达式(generator expression)"></a>生成器表达式(generator expression)</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">gen = (i*i <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">2</span>, <span class="number">4</span>))</div><div class="line">gen</div><div class="line"><span class="comment"># &lt;generator object &lt;genexpr&gt; at 0x10f6e2af0&gt;</span></div><div class="line">gen.__next__()</div><div class="line"><span class="comment"># 4</span></div><div class="line">gen.__next__()</div><div class="line"><span class="comment"># 9</span></div><div class="line">gen.__next__()</div><div class="line"><span class="comment"># StopIteration</span></div></pre></td></tr></table></figure>
<h3 id="三个内置方法"><a href="#三个内置方法" class="headerlink" title="三个内置方法"></a>三个内置方法</h3><h4 id="close"><a href="#close" class="headerlink" title="close()"></a>close()</h4><p><code>close()</code>方法可以关闭生成器。生成器被关闭后，再次调用<code>__next__()</code>方法，不管能否遇到yield关键字，都会立即抛出StopIteration异常。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">gen = (i*i <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">2</span>, <span class="number">4</span>))</div><div class="line">gen.close()</div><div class="line">gen.__next__()</div><div class="line"><span class="comment"># StopIteration</span></div></pre></td></tr></table></figure>
<h4 id="send"><a href="#send" class="headerlink" title="send()"></a>send()</h4><p>可以通过<code>send()</code>方法，向生成器内部传递参数。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">consumer</span><span class="params">()</span>:</span></div><div class="line">    print(<span class="string">'ready to receive msg'</span>)</div><div class="line">    <span class="keyword">while</span> <span class="keyword">True</span>:</div><div class="line">        msg = <span class="keyword">yield</span></div><div class="line">        print(<span class="string">'got msg: %s'</span> % msg)</div><div class="line"></div><div class="line">con = consumer()</div><div class="line">con.__next__() <span class="comment"># con.send(None)</span></div><div class="line"><span class="comment"># ready to receive msg</span></div><div class="line">con.send(<span class="string">'msg 1'</span>)</div><div class="line"><span class="comment"># got msg: msg 1</span></div><div class="line">con.send(<span class="string">'msg 2'</span>)</div><div class="line"><span class="comment"># got msg: msg 2</span></div><div class="line">con.close()</div><div class="line">con.send(<span class="string">'msg 3'</span>)</div><div class="line"><span class="comment"># StopIteration</span></div></pre></td></tr></table></figure>
<p>需要注意的是，调用<code>send()</code>方法之前，必须先调用<code>__next__()</code>方法或者<code>send(None)</code>，让生成器执行到<code>yield</code>处以接受参数。否则将抛出异常<code>can&#39;t send non-None value to a just-started generator</code>。其实<code>__next()__</code>的实现，就是<code>send(None)</code>。</p>
<h4 id="throw"><a href="#throw" class="headerlink" title="throw()"></a>throw()</h4><p>除了向生成器函数内部传递参数，我们还可以传递异常。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">throw_gen</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">try</span>:</div><div class="line">        <span class="keyword">yield</span> <span class="string">'Normal'</span></div><div class="line">    <span class="keyword">except</span> ValueError:</div><div class="line">        <span class="keyword">yield</span> <span class="string">'Error'</span></div><div class="line">    <span class="keyword">finally</span>:</div><div class="line">        print(<span class="string">'Finally'</span>)</div><div class="line"></div><div class="line"></div><div class="line">gen = throw_gen()</div><div class="line">print(gen.__next__())  </div><div class="line"><span class="comment"># Normal</span></div><div class="line">print(gen.__next__())  </div><div class="line"><span class="comment"># Finally </span></div><div class="line"><span class="comment"># StopIteration</span></div></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">gen = throw_gen()</div><div class="line"><span class="keyword">print</span> gen.next()  </div><div class="line"><span class="comment"># Normal</span></div><div class="line"><span class="keyword">print</span> gen.throw(ValueError)    </div><div class="line"><span class="comment"># Error</span></div><div class="line"><span class="keyword">print</span> gen.next()  </div><div class="line"><span class="comment"># Finally</span></div><div class="line"><span class="comment"># StopIteration</span></div></pre></td></tr></table></figure>
<h1 id="todo"><a href="#todo" class="headerlink" title="todo"></a>todo</h1><ul>
<li>yield from</li>
<li>Coroutine</li>
<li>Asynchronous generator functions</li>
<li>Awaitable, AsyncIterator, AsyncIterable</li>
</ul>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><ul>
<li><a href="http://kuanghy.github.io/2016/05/18/python-iteration" target="_blank" rel="external">http://kuanghy.github.io/2016/05/18/python-iteration</a></li>
<li><a href="https://eastlakeside.gitbooks.io/interpy-zh/content/Generators/" target="_blank" rel="external">https://eastlakeside.gitbooks.io/interpy-zh/content/Generators/</a></li>
<li><a href="https://docs.python.org/3/tutorial/classes.html#iterators" target="_blank" rel="external">https://docs.python.org/3/tutorial/classes.html#iterators</a></li>
<li><a href="http://www.cnblogs.com/huxi/archive/2011/07/01/2095931.html" target="_blank" rel="external">http://www.cnblogs.com/huxi/archive/2011/07/01/2095931.html</a></li>
<li><a href="https://foofish.net/iterators-vs-generators.html" target="_blank" rel="external">https://foofish.net/iterators-vs-generators.html</a></li>
<li><a href="http://nvie.com/posts/iterators-vs-generators/" target="_blank" rel="external">http://nvie.com/posts/iterators-vs-generators/</a></li>
<li><a href="http://pyzh.readthedocs.io/en/latest/the-python-yield-keyword-explained.html#id3" target="_blank" rel="external">http://pyzh.readthedocs.io/en/latest/the-python-yield-keyword-explained.html#id3</a></li>
<li><a href="http://www.bjhee.com/python-yield.html" target="_blank" rel="external">http://www.bjhee.com/python-yield.html</a></li>
</ul>

  </div>
</article>



      </div>
    <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2017 enomine
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="http://github.com/enominecn">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    <script src="/lib/justified-gallery/jquery.justifiedGallery.min.js"></script>
<script src="/js/main.js"></script>
<!-- Google Analytics -->

    <script type="text/javascript">
        (function(i,s,o,g,r,a,m) {i['GoogleAnalyticsObject']=r;i[r]=i[r]||function() {
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
        ga('create', 'UA-86660611-1', 'auto');
        ga('send', 'pageview');
    </script>

<!-- Disqus Comments -->


</body>
</html>
